package com.jxzn.locust.service.business;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jxzn.locust.dao.RoleResourceRepository;
import com.jxzn.locust.model.RoleResource;
import com.jxzn.locust.model.SysResource;
import com.jxzn.locust.support.Auth;
import com.jxzn.locust.support.LoginSession;
import com.jxzn.locust.support.Module;

@Service
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    RoleResourceRepository roleResourceRepository;

    @Override
    public boolean checkSystemPermission(HttpSession session, Module module, Auth auth) {
        int roleId = NumberUtils.toInt(LoginSession.getItem(session, LoginSession.ROLE_ID), 0);
        if (roleId == 0) {
            return false;
        }
        String supper = LoginSession.getItem(session, LoginSession.SUPPER);
        if (Boolean.valueOf(supper)) {
            return true;
        }
        SysResource resource = new SysResource(module, auth);
        RoleResource rr = roleResourceRepository.findByRoleIdAndResourceUuid(roleId, resource.getKey());
        return rr != null;
    }

}
